package com.zang.server.controller;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.zang.server.annotation.OptLog;
import com.zang.server.pojo.Admin;
import com.zang.server.pojo.AdminLoginParam;
import com.zang.server.pojo.RespBean;
import com.zang.server.pojo.Role;
import com.zang.server.service.IAdminService;
import com.zang.server.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.zang.server.constanct.OptTypeConst.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zang
 * @since 2021-12-28
 */
@Api(tags = "操作员模块")
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    private IAdminService iAdminService;
    @Autowired
    private IRoleService iRoleService;

    @ApiOperation(value = "获取所有操作员")
    @GetMapping("/")
    public List<Admin> getAllAdmins( String keywords){
        return iAdminService.getAllAdmins(keywords);
    }
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "更新操作员")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin){
        boolean b = iAdminService.updateById(admin);
        if (b){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }
    @OptLog(optType = SAVE)
    @ApiOperation(value = "添加操作员")
    @PostMapping("/")
    public RespBean insertAdmin(@RequestBody AdminLoginParam adminLoginParam){
        boolean b = iAdminService.insertAdmin(adminLoginParam);
        if (b){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable("id") Integer id){
        boolean b = iAdminService.removeById(id);
        if (b){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
    @ApiOperation("获取所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        return iRoleService.list();
    }
    @OptLog(optType = UPDATE)
    @ApiOperation("更新操作员角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId,Integer[] rids){
        return iAdminService.updateAdminRole(adminId,rids);
    }

}
